#include"udp_server.hpp"
#include <stdlib.h>
#include <ctype.h>
#include<memory>
#include <stdio.h>
using namespace udp_serve; 
//./server port
void usage(const std::string proc)
{
    std::cout<<"Usage "<<proc<<" port"<<std::endl;
}
std::string transstring(std::string request)
{
    std::string result;
    char c;
    for(auto& r : request)
    {
        if(islower(r))
        {    
            c = toupper(r);
            result += c;
        }
        else
            result += r;
    }
    return result;
}
bool issafe(std::string Command)
{
    bool ret = true;
    if(Command.find("rm") != -1)
        ret = false;
    if(Command.find("while") != -1)
        ret = false;
    if(Command.find("mv") != -1)
        ret = false;
    if(Command.find("touch") != -1)
        ret = false;

    return ret;
}
std::string Command(std::string command)
{
    //1 命令检查
    if(!issafe(command))
        return "Unsafe";
    //内部创建子进程执行命令，并将结果写到文件
    FILE* file = popen(command.c_str(),"r");
    //读取结果
    if(file == nullptr)
        return "none";
    char buffer[1024] = {0};
    std::string ret;
    while(fgets(buffer,sizeof(buffer),file))
    {
        ret += buffer;
    }    
    fclose(file);
    return ret;
}
class B
{
public:
    void log()
    {
        std::cout<<"log"<<std::endl;
    }

};
#include<functional>
int main(int argc,char*argv[])
{
    if(argc != 2)
    {
        usage(argv[1]);
        std::cout<<"port error"<<strerror(errno)<<std::endl;
        exit(USAGE_ERR);
    }
    u_int16_t port = atoi(argv[1]);
    // std::shared_ptr<UdpServer> udp = std::make_shared<UdpServer>("1.1.1.1",8080);
    // std::shared_ptr<UdpServer> udp = std::make_shared<UdpServer>(transstring,port);
    // std::shared_ptr<UdpServer> udp = std::make_shared<UdpServer>(Command,port);

    std::shared_ptr<UdpServer> udp = std::make_shared<UdpServer>(port);
    // udp->InitServer();
    udp->start();
    return 0;
}